package homework.gui;

import com.lovo.netCRM.component.*;
import homework.bean.ClassBean;
import homework.bean.StudentBean;
import homework.util.DataUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class AddStudentDialog extends JDialog {
      //文本输入框
      private LovoTxt nameTxt,ageTxt;
      //单选按钮
     private LovoRadioButton genderRadio;
     //下拉菜单
    private LovoComboBox  classList;
    //添加按钮
    private LovoButton addBtn;

    private LovoFileChooser lovoFileChooser;


    private StudentFrame studentFrame;

    public AddStudentDialog(StudentFrame studentFrame){

        this.studentFrame = studentFrame;

        this.setSize(600,400);
        this.setLayout(null);

        nameTxt =  new LovoTxt("姓名",50,100,this);
        ageTxt =  new LovoTxt("年龄",50,150,this);
        genderRadio = new LovoRadioButton("性别",new String[]{"男","女"},50,200,this);
        //查询所有的班级
        List<ClassBean> list = DataUtil.getAllClasses();
        classList = new LovoComboBox(list,"所在班级",50,250,this);

        lovoFileChooser = new LovoFileChooser(this);
        lovoFileChooser.setBounds(300,100,100,150);

        addBtn = new LovoButton("添加",50,300,this);

        //添加按钮
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //分别获得输入控制的值
                String name = nameTxt.getText();
                 int age = Integer.parseInt(ageTxt.getText());
                 String gender = genderRadio.getItem();
                 ClassBean classBean = (ClassBean) classList.getItem();
                 //封装学生对象
                StudentBean studentBean = new StudentBean();
                studentBean.setStudentId(DataUtil.nextId);
                studentBean.setName(name);
                studentBean.setGender(gender);
                studentBean.setAge(age);
                studentBean.setClassBean(classBean);

                String path = lovoFileChooser.getFilePath();
                if(path == null){
                    JOptionPane.showMessageDialog(null, "请选择上传的头像");
                    return;
                }else{
                    if(!path.substring(path.lastIndexOf(".")).
                            matches("\\.(png|jpg|gif)")){
                        JOptionPane.showMessageDialog(null, "图片格式不正确");
                        return;
                    }else{
                       // DataUtil.savePicture(path);
                        studentBean.setPicture(path);
                        //把封装好的对象保存到数据工具类中
                        DataUtil.addStudent(studentBean);
                        //得到更新的数据
                        List<StudentBean> newlist = DataUtil.getAllStudents();
                        //更新表格的显示数据
                        studentFrame.getShowTablePanel().getDatas().updateLovoTable(newlist);
                        JOptionPane.showMessageDialog(null,"成功");
                    }
                }
                //销毁添加对话框
                AddStudentDialog.this.dispose();

            }
        });


        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}
